package com.dtborad.myblog.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dtborad.myblog.common.CommonErrorCode;
import com.dtborad.myblog.common.Result;
import com.dtborad.myblog.entity.Categories;
import com.dtborad.myblog.entity.Posts;
import com.dtborad.myblog.entity.Tags;
import com.dtborad.myblog.service.PostService;
import com.dtborad.myblog.service.TagService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/tag")
@Api(tags = "标签模块")
@Slf4j
public class TagController {

    @Autowired
    private TagService tagService;

    @Autowired
    private PostService postService;

    //分页
    @GetMapping("/list")
    public Result<Map<String, Object>> list(@RequestParam(defaultValue = "1") int pagenum,
                                            @RequestParam(defaultValue = "5") int pagesize,
                                            @RequestParam(defaultValue = "") String tagname) {
        Page<Tags> currentPage = tagService.getAllTagsByPageSize(pagenum, pagesize, tagname);
        Map<String, Object> response = new HashMap<>();
        response.put("records", currentPage.getRecords());
        response.put("total", currentPage.getTotal());
        return Result.ofSuccess(response);
    }

    //新增
    @PostMapping("/add")
    public Result<String> add(@RequestBody Tags tag) {
        try {
            Tags tags = new Tags();
            //将传递参数的属性拷贝到新对象中
            BeanUtils.copyProperties(tag, tags);
            tags.setCreatetime(new Date());
            //保存新对象
            tagService.save(tags);
        } catch (Exception e) {
            return Result.ofFail(CommonErrorCode.FAILURE);
        }
        return Result.ofSuccess();
    }

    //修改
    @PostMapping("/update")
    public Result<String> update(@RequestBody Tags tag) {
        try {
            LambdaQueryWrapper<Tags> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Tags::getTagid, tag.getTagid());
            Tags one = tagService.getOne(queryWrapper);
            if (one != null) {
                one.setTagname(tag.getTagname());
                tagService.update(one, queryWrapper);
                return Result.ofSuccess();
            }
        } catch (Exception e) {
            return Result.ofFail(CommonErrorCode.INVALID_PARAM);
        }
        return Result.ofFail(CommonErrorCode.FAILURE);
    }

    //删除
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable("id") int id) {
        try {
            //查询分类是否关联文章
            LambdaQueryWrapper<Tags> wrapper = new LambdaQueryWrapper();
            wrapper.eq(Tags::getTagid, id);
            Tags one = tagService.getOne(wrapper);
            LambdaQueryWrapper<Posts> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.like(Posts::getTags, one.getTagname());
            List<Posts> list = postService.list(queryWrapper);
            if (list != null && !list.isEmpty()) {
                for (int i = 0; i < list.size(); i++) {
                    Posts posts = list.get(i);
                    StringBuilder tags = new StringBuilder();
                    tags.append(posts.getTags());
                    //移除此标签
                    tags.delete(tags.indexOf(one.getTagname()), tags.indexOf(one.getTagname()) + one.getTagname().length());
                    posts.setTags(tags.toString());
                    postService.updatePostById(posts);
                }
            }
            tagService.removeById(id);
            return Result.ofSuccess();
        } catch (Exception e) {
            return Result.ofFail(CommonErrorCode.FAILURE);
        }
    }


}
